package com.order.demo;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private OrderMapper mapper;
    @Autowired
    private DiscoveryClient discoveryClient;
    @LoadBalanced
    private RestTemplate restTemplate = new RestTemplate();
    private AtomicInteger count = new AtomicInteger(1);
    private List<ServiceInstance> instances;

    @PostConstruct
    public void init(){
        instances = discoveryClient.getInstances("product-service");
    }
//    @RequestMapping("/{id}")
//    public OrderInfo getOrderById(@PathVariable("id") Integer Id){
//        OrderInfo orderInfo = mapper.selectOrderById(Id);
//        //String url = "http://127.0.0.1:10011/product/" + orderInfo.getProductId();
//        // 从eureka中获取服务列表
//        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
//        String uri = instances.get(0).getUri().toString();
//        System.out.println(uri + "  ++++++++++++++++++++++++");
//        String url = uri + "/product/" + orderInfo.getProductId();
//        System.out.println(url + "  ++++++++++++++++++++++++");
//        ProductInfo forObject = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(forObject);
//        return orderInfo;
//    }

//    @RequestMapping("/{id}")
//    public OrderInfo getOrderById(@PathVariable("id") Integer Id){
//        OrderInfo orderInfo = mapper.selectOrderById(Id);
//        //String url = "http://127.0.0.1:10011/product/" + orderInfo.getProductId();
//        // 从eureka中获取服务列表
//        int index = count.getAndIncrement() % instances.size();
//        String uri = instances.get(index).getUri().toString();
//        System.out.println(uri + "  ++++++++++++++++++++++++");
//        String url = uri + "/product/" + orderInfo.getProductId();
//        System.out.println(url + "  ++++++++++++++++++++++++");
//        ProductInfo forObject = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(forObject);
//        return orderInfo;
//    }

    @RequestMapping("/{id}")
    public OrderInfo getOrderById(@PathVariable("id") Integer Id){
        OrderInfo orderInfo = mapper.selectOrderById(Id);
        String url = "http://127.0.0.1:10011/product/" + orderInfo.getProductId();
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
    }
}
